#!/bin/sh
# - runtime
# - nr_threads
# - ip
# - test
# - send_size
# - socket_buffer_size
# - delay
# - iterations
# - testnames
# - TCP_STREAM_send_sizes
# - UDP_STREAM_send_sizes

## Netperf is a benchmark that can be use to measure various aspect of
## networking performance. The primary foci are bulk (aka unidirectional)
## data transfer and request/response performance using either TCP
## or UDP and the Berkeley Sockets interface.
. $LKP_SRC/lib/env.sh
. $LKP_SRC/lib/reproduce-log.sh
. $LKP_SRC/lib/wait.sh
setup_wait

cat > /etc/sysconfig/network-scripts/ifcfg-enp189s0f3 << EOF
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEM_MODE=eui64
NAME=enp189s0f3
DEVICE=enp189s0f3
ONBOOT=yes
IPADDR=192.168.222.2
NETMASK=255.255.255.0
GATEWAY=192.168.222.2
EOF
sudo ifdown enp189s0f3
sudo ifup enp189s0f3

pkgname=netperf
pkgver=2.7
pkgrel=0
source="https://github.com/HewlettPackard/netperf/archive/$pkgname-$pkgver.$pkgrel.tar.gz"

build() {
    ip route add default via 172.168.131.2
    yum install -y tar libpcap-devel gcc-c++ autoconf automake libtool make firewalld
    mkdir -p /netperf-source
    cd /netperf-source
    wget $source
    tar xvf $pkgname-$pkgver.$pkgrel.tar.gz
    cd $pkgname-$pkgname-$pkgver.$pkgrel

    ./autogen.sh

    ./config.guess > /dev/null 2>&1 || {
        curl -s -o config.guess 'http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD'
        curl -s -o config.sub 'http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=HEAD'
    }

    sed -i 's/(netperf_sendfile/(len = netperf_sendfile/g' src/nettest_bsd.c

    if [[ "$arch" == "aarch64" ]];then
        ./configure CFLAGS=-fcommon --host=arm-linux --build=arm-linux
    fi

    if [[ "$arch" == "x86_64" ]];then
        ./configure CFLAGS=-fcommon --host=x86-linux --build=x86-linux
    fi

    make -j
	
    systemctl stop firewalld
    export PATH="/netperf-source/$pkgname-$pkgname-$pkgver.$pkgrel/src/:$PATH"
}

[ "${test#*SCTP}" != "$test" ] && log_cmd modprobe sctp 2>/dev/null
sleep 1

[ -n "$send_size" ] && send_size="-m $send_size"
[ -n "$socket_buffer_size" ] && socket_buffer_size="-s $socket_buffer_size -S $socket_buffer_size"

if [ -n "$send_size" ] || [ -n "$socket_buffer_size" ]; then
    test_options="-- $send_size $socket_buffer_size"
fi

[ -n "$direct_server_ips" ] && server=$direct_server_ips
[ -z "$server" ] && {
    if [ "$ip" = 'ipv6' ]; then
        server=::1
    else
        server=127.0.0.1
    fi
}
server=${server%% *}

opt_ip=
[ "$ip" = 'ipv4' ] && opt_ip='-4'
[ "$ip" = 'ipv6' ] && opt_ip='-6'

run_netperf_performance()
{
    . $LKP_SRC/setup/drop_caches
    if [[ "$test" =~ .*_*RR ]];then
        log_eval "use_benchmark_env /lkp/benchmarks/netperf/bin/netperf $opt_ip -H $server -t $test &"
    elif [ "$test" = "TCP_STREAM" ] || [ "$test" = "UDP_STREAM" ];then
        log_eval "use_benchmark_env /lkp/benchmarks/netperf/bin/netperf $opt_ip -H $server -t $test -l 60 -- $send_size -R 1 &"
    fi
}

run_netperf()
{
    for i in $(seq $nr_threads)
    do
        log_eval "netperf $opt_ip -H $server -t $test -c -C -l $runtime $test_options &"
    done
}

run_netperf_all()
{
    [ -n "$delay" ] && $WAIT_POST_TEST_CMD --timeout "$delay"

    if [ "$is_performance_dashboard" = "true" ]; then
        run_netperf_performance
    else
        run_netperf
    fi
    log_eval wait
}

run_for_iterations()
{
    for i in $(seq 1 $iterations); do
        echo "Iterations: $i"
        run_netperf
    done
}

build

if [ -n "$test" ]; then
    if [ -n "$iterations" ]; then
        run_for_iterations
    else
        run_netperf_all
    fi
else
    for test in $testnames; do
        if [ "$test" = "TCP_STREAM" ];then
            for size in $TCP_STREAM_send_sizes; do
                log_eval "netperf -t $test -H $server -l 60 -- -m $size"
            done
        elif [ "$test" = "UDP_STREAM" ];then
            for size in $UDP_STREAM_send_sizes; do
                log_eval "netperf -t $test -H $server -l 60 -- -m $size"
            done
        else
            log_eval "netperf -t $test -H $server"
        fi
    done
fi

# Comments from Jesse Brandeburg <jesse.brandeburg@intel.com>
# -P0 -- -r 64k -b 10
# -T0,0
